﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace KadalaSpree.XCore
{
    public static partial class X
    {
        public class Logger
        {
            public static readonly log4net.ILog BaseLogging = Zeta.Common.Logger.GetLoggerInstanceForType();

            private Action<string, object[]> LogAction { get; set; }
            private Action<string, object[]> DebugAction { get; set; }

            public Logger ()
            {
                Initialize(new XModule());
            }

            public Logger(XModule module)
            {
                var stamp = string.Format("[ {0} {1} {2} ] ", module.Plugin.Name, module.Plugin.Version, module.Name);
                stamp = Regex.Replace(stamp, " {2,}", " ");

                Initialize(module, stamp);
            }

            private void Initialize(XModule module, string stamp = "")
            {
                Action<string, object[]> log = delegate(string message, object[] args)
                {
                    BaseLogging.Info(stamp + string.Format(message, args));
                };

                Action<string, object[]> debug = delegate(string message, object[] args)
                {
                    if (module.Plugin != null && module.Plugin.IsDebug)
                    {
                        BaseLogging.Info(stamp + string.Format(message, args));
                    }
                };

                LogAction = log;
                DebugAction = debug;
            }

            public void Log (string s, params object[] args)
            {
                LogAction(s, args);
            }

            public void Debug(string s, params object[] args)
            {
                DebugAction(s, args);
            }

            public void Raw (string s)
            {
                BaseLogging.Info(s);
            }

            public void RawFormat (string s, params object[] args)
            {
                BaseLogging.InfoFormat(s, args);
            }

            private static Logger _instance;
            public static Logger Instance
            {
                get { return _instance ?? (_instance = new Logger()); }
            }

        }
    }
}
